package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.LineAdapter;
import com.sxx.beautifulcity.database.LineHelper;
import com.sxx.beautifulcity.dialog.CommonProgressDialog;
import com.sxx.beautifulcity.entity.greendao.LineDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnLineItemClickListener;
import com.sxx.beautifulcity.constant.Constant;
import com.sxx.beautifulcity.popmenu.CommonPopMenu;
import com.sxx.beautifulcity.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/29.
 */
public class LineActivity extends AppCompatActivity implements View.OnClickListener, OnLineItemClickListener {
    private RecyclerView recyclerView;
    private final static String TAG = "LineActivity";
    private LineAdapter adapter;
    private TextView titleTv, moreTv;
    private ImageView backIv;
    private LineHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_line);
        init();
        helper = new LineHelper();
        List<LineDaoEntity> daoLine = helper.queryAllLine();
        if (daoLine.size() == 0) {
            requestLineContent();
        } else {
            adapter.addData(daoLine);
        }


    }

    /***
     * 初始化
     */
    private void init() {
        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("精品线路");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(this);
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");
        recyclerView = (RecyclerView) findViewById(R.id.recyclerviewLine);
        adapter = new LineAdapter();
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setOnLineItemClick(this);
    }

    /***
     * 请求网络数据
     */
    private void requestLineContent() {
        final Dialog dialog = CommonProgressDialog.createLoadingDialog(this, "Loading......");
        String path = Constant.GoodLINE_URL;
        JsonObjectRequest request = new JsonObjectRequest(path, null,
                new Response.Listener<JSONObject>() {
                    Gson gson = new Gson();
                    List<LineDaoEntity> datas = new ArrayList<>();

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("list_data")) {
                            try {
                                JSONArray list_data = response.getJSONArray("list_data");
                                for (int i = 0; i < list_data.length(); i++) {
                                    String str = list_data.getString(i);
                                    LineDaoEntity data = gson.fromJson(str, LineDaoEntity.class);
                                    datas.add(data);
                                }
                                adapter.addData(datas);
                                helper.deleteLine(datas);
                                helper.saveLineList(datas);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return super.getHeaders();
            }
        };
        VolleySingleton.getInstance().addRequest(request, TAG);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backIV:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                break;
        }

    }

    @Override
    public void OnLineItemClick(LineDaoEntity data) {
        Intent intent = new Intent(this, LineDetailActivity.class);
        intent.putExtra("id", data.getId());
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.left_out, R.anim.right_into);
    }
}
